package com.jasonchoi.rabbitmq.producter;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.jasonchoi.rabbitmq.config.DirectRabbitConfig.TEST_DIRECT_EXCHANGE;
import static com.jasonchoi.rabbitmq.config.DirectRabbitConfig.TEST_DIRECT_ROUTING;
import static com.jasonchoi.rabbitmq.config.FanoutRabbitConfig.FANOUT_EXCHANGE;
import static com.jasonchoi.rabbitmq.config.TopicRabbitConfig.FIRST_TEST_TOPIC;
import static com.jasonchoi.rabbitmq.config.TopicRabbitConfig.SECOND_TEST_TOPIC;
import static com.jasonchoi.rabbitmq.config.TopicRabbitConfig.TOPIC_EXCHANGE;

/**
 * @Author: JasonChoi
 * @Date: 2020/5/28 16:06
 */
@Component
public class TestProducter {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void directSend(String msg) {
        rabbitTemplate.convertAndSend(TEST_DIRECT_EXCHANGE, TEST_DIRECT_ROUTING, msg);
    }

    public void topicSend(String msg, boolean broadcast) {
        if (broadcast) {
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE, FIRST_TEST_TOPIC, msg);
        } else {
            rabbitTemplate.convertAndSend(TOPIC_EXCHANGE, SECOND_TEST_TOPIC, msg);
        }
    }

    public void fanoutSend(String msg) {
        rabbitTemplate.convertAndSend(FANOUT_EXCHANGE, null, msg);
    }
}
